Linux 使用 ADSL (計時制) 的經驗分享

前言

自從去年 (民國 88 年) 底,Hinet 推出 ADSL 之後,便廣受各方矚目,再加上今年 (民國 89 年) 四月底推出降價的優惠方案,使得申請人數暴增,當然此時各大 ISP 業者亦陸續推出 ADSL 的業務。其中 ADSL 又可分為固定制與計時制二種,前者具有費用較高及擁有固定 IP 的優點,後者費用較低,但採用動態分配 IP 的方式。換句話說,計時制的用戶,雖然有合法的 IP 可用,但每次撥接到 ISP 後,所取得的 IP 並不相同。因此,若有意架站 (提供某些伺服器的功能,例如:Web、Mail、FTP... 等) 的公司或個人,便申請固定制,其餘則申請計時制。

由於計時制的申請人數較多,而且計時制必須撥接上網,因此本文特別針對 ADSL 計時制的特性,加以說明如何在 Linux 的系統下使用。另外,由於計時制的 IP 並不固定,因此本文也將提供二個使用的技巧,讓計時制的用戶也能像固定制的用戶般的方便。當然還是會有些差異,但就我個人而言,我認為這樣便足夠了,不需另外每個月花 800 元 (目前 Hinet 固定制與計時制的差價) 來申請固定制。

 
安裝與設定

在開始安裝之前,首先說明,以下所提到的操作與設定,均是以 RedHat 6.2 的環境為主,為免因各種 Linux 套件所產生的差異性,而造成無法順利執行,故在此先聲明。

由於 ADSL 計時制是採用 Point-to-Point Over Ethernet (簡稱 PPPoE) 的通訊協定 (詳細內容請參考 RFC2516 的說明) 來撥接到 ISP,故我們必須要先找到一套可以在 Linux 下執行的 PPPoE Client,這類的軟體可以在 freshmeat.net 找到好幾個,在本文將以 RP-PPPoE 為例。

RP-PPPoE 這套軟體可以在 http://www.roaringpenguin.com/pppoe/ 找到相關的說明,目前最新的版本是 2.2。按 這裡 可以下載所需的 rp-pppoe-2.2-1.i386.rpm 檔案。

系統需求

  • Linux Kernel 2.0 或 2.2 系列
  • Kernel 要支援 packet sockets。(預設已經開啟此功能)
  • pppd 2.3.7 或更新版。(如果要使用 demand-dialing 則要求 pppd 2.3.10 或更新版)
  • Ethernet 網路卡
  • ADSL Modem (由中華電信提供)

開始安裝

  1. 先在 Linux 中將網路卡的相關設定完成 (在 conf.modules 檔案中設定,或者藉由 netconf 程式來設定)。另請要注意一點,要接 ADSL ATU-R 的那片網路卡不要設定 IP 等相關資料。要檢查是否設定正確,可執行下列指令查看:

    [root@linux /root]# ifconfig -a
    eth0    Link encap:Ethernet HWaddr FE:FF:FF:FF:FF:FF
          inet addr:192.168.29.254 Bcast:192.168.29.255 Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
          RX packets:1964 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1372 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          Interrupt:10 Base address:0x300

    eth1    Link encap:Ethernet HWaddr 00:80:C8:7B:AE:D8
          BROADCAST MULTICAST MTU:1500 Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          Interrupt:5 Base address:0x320

    lo      Link encap:Local Loopback
          inet addr:127.0.0.1 Mask:255.0.0.0
          UP LOOPBACK RUNNING MTU:3924 Metric:1
          RX packets:229 errors:0 dropped:0 overruns:0 frame:0
          TX packets:229 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0

    你應該看到與上面類似的訊息,其中 eth0 與 eth1 分別是我的二張網路卡,而 eth1 是要用來接 ATU-R 的網路卡,所以雖然我有載入此網路卡的模組,但並未設定 IP。(如果你根本未看到你所設定的網路卡出現,或者其 HWaddr 後面未顯示該網路卡的 MAC 位址,那代表你的設定有問題,須再檢查看看。)

  2. 檢查是否已經安裝 pppd。

    [root@linux /root]# rpm -qa | grep ppp

    如果出現類似 ppp-2.3.11-4 這樣的訊息,則表示已安裝 pppd,請跳到步驟 3。

  3. 安裝 pppd。(RedHat 6.2 中的版本為 ppp-2.3.11-4.i386.rpm)

    [root@linux /root]# rpm -ihv ppp-2.3.11-4.i386.rpm

  4. 安裝 rp-pppoe-2.2-1.i386.rpm。

    [root@linux /root]# rpm -ihv rp-pppoe-2.2-1.i386.rpm

設定

執行下列指令,並依說明輸入相關的資料:

[root@linux /root]# adsl-setup

Welcome to the Roaring Penguin ADSL client setup. First, I will run
some checks on your system to make sure the PPPoE client is installed
properly...

Looks good! Now, please enter some information:

USER NAME

>>> Enter your PPPoE user name (default bxxxnxnx@sympatico.ca): 84xxxxxx@hinet.net
(輸入 ISP 給你的帳號,格式如範例如示。)

INTERFACE

>>> Enter the Ethernet interface connected to the ADSL modem
(default eth1): eth1
(輸入連接 ADSL ATU-R 的網路卡名稱,例如:eth0)

Do you want the link to come up on demand, or stay up continuously?
If you want it to come up on demand, enter the idle time in seconds
after which the link should be dropped. If you want the link to
stay up permanently, enter 'no' (two letters, lower-case.)
NOTE: Demand-activated links do not interact well with dynamic IP
addresses. You may have some problems with demand-activated links.
>>> Enter the demand value (default no): no
(如果你希望 ADSL 持續連線,則回答 no,否則如果希望一段時間未使用便斷線,則輸入你希望的時間,單位為秒。)

DNS

Please enter the IP address of your ISP's primary DNS server.
If your ISP claims that 'the server will provide DNS addresses',
enter 'server' (all lower-case) here.
If you just press enter, I will assume you know what you are
doing and not modify your DNS setup.
>>> Enter the DNS information here: 168.95.1.1
(輸入你的 ISP 所提供的主要的 DNS 伺服器的 IP,Hinet 的用戶可輸入 168.95.1.1)

Please enter the IP address of your ISP's secondary DNS server.
If you just press enter, I will assume there is only one DNS server.
>>> Enter the secondary DNS server address here:168.96.192.1
(輸入你的 ISP 所提供的次要的 DNS 伺服器的 IP,Hinet 的用戶可輸入 168.95.192.1)

PASSWORD

>>> Please enter your PPPoE password:
>>> Please re-enter your PPPoE password:
(輸入 ISP 所核發給你的密碼,要連續輸入二次,且二次的密碼要相同。)

FIREWALLING

Please choose the firewall rules to use. Note that these rules are
very basic. You are strongly encouraged to use a more sophisticated
firewall setup; however, these will provide basic security. If you
are running any servers on your machine, you must choose 'NONE' and
set up firewalling yourself. Otherwise, the firewall rules will deny
access to all standard servers like Web, e-mail, ftp, etc. If you
are using SSH, the rules will block outgoing SSH connections which
allocate a privileged source port.

The firewall choices are:
0 - NONE: This script will not set any firewall rules. You are responsible
    for ensuring the security of your machine. You are STRONGLY
    recommended to use some kind of firewall rules.
1 - STANDALONE: Appropriate for a basic stand-alone web-surfing workstation
2 - MASQUERADE: Appropriate for a machine acting as an Internet gateway
        for a LAN
>>> Choose a type of firewall (0-2): 0
(如果這部 Linux 的機器是擔任 Gateway 的角色,則選擇 2,這樣內部的機器便可透過此 Linux 機器,連結到 Internet。如果此 Linux 機器是獨立的機器,則可選擇 1,這樣除了這部 Linux 可連結到 Internet 之外,還會有基本 Firewall 的功能。當然你也可以選擇 0,這樣此 Linux 只負責撥接到 ISP,而沒有任何 Firewall 的功能。在這裡我選擇 0,因為我打算自己使用 ipchains 及 ipmasqadm 來設定我的 Firewall 的功能。但這樣做會比較麻煩(換個角度來說,這樣做比較有樂趣,而且學得到東西),如果你不想這樣做,你可以選擇 1 或 2。)

** Summary of what you entered **

Ethernet Interface: eth1
User name: 84025008@hinet.net
Activate-on-demand: No
Primary DNS: 168.95.1.1
Secondary DNS: 168.95.192.1
Firewalling: NONE >>>

Accept these settings and adjust configuration files (y/n)? y
(最後檢查一下上面所列出來的資料是否有誤,如果正確無誤,請輸入 y 將之前的設定存檔,否則請按 n 並重新執行 adsl-setup 來設定。)

Adjusting /etc/ppp/pppoe.conf
Adjusting /etc/resolv.conf
 (But first backing it up to /etc/resolv.conf-bak)
Adjusting /etc/ppp/pap-secrets and /etc/ppp/chap-secrets
 (But first backing it up to /etc/ppp/pap-secrets-bak)
 (But first backing it up to /etc/ppp/chap-secrets-bak)

Congratulations, it should be all set up!
Type 'adsl-start' to bring up your ADSL link and 'adsl-stop' to bring
it down. Type 'adsl-status' to see the link status.

   
使用
 

完成以上的步驟之後,如果一切順利的話,我們便可以開始利用 ADSL 來享受高速上網的樂趣。(說真的,和過去用 56K Modem 相比,我覺得蠻快的!!)

啟動連線

[root@linux /root]# adsl-start
. Connected!

檢查連線狀態

[root@linux /root]# adsl-status
adsl-status: Link is up and running on interface ppp0
ppp0   Link encap:Point-to-Point Protocol inet addr:168.95.186.53 P-t-P:168.95.186.254 Mask:255.255.255.255
     UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1
     RX packets:18 errors:0 dropped:0 overruns:0 frame:0
     TX packets:15 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:10

結束連線

[root@linux /root]# adsl-stop
Killing adsl-connect (1046)
Killing pppd (1058)

最後,記得執行 setup 來設定 adsl 的服務要自動啟動。

 
如何讓多部 PC 透過 Linux 連到 Internet

當完成前面的步驟之後,Linux 便可藉由 ADSL 的方式連到 Internet。但若你有多部 PC 要同時上網,總不能每部都申請一條 ADSL 吧?! 那可要花不少錢。這一小節便說明,如何透過 Linux 來連接 Internet。

首先,你必須再 Linux 另外安裝一片網路卡,當然所有要上網的 PC 也都要有一片網路卡,最後再買一個 Hub,來將這幾部 PC (包括 Linux 上的那片網路卡) 接到 Hub,如此硬體架構便算完成。

接下來,你可以從選擇一組保留未用的私有 IP (例如:192.168.1.x),自行分配給每部機器,例如:

Linux 192.168.1.254/24
PC1   192.168.1.1/24
PC2   192.168.1.2/24
..... 以此類推

當然記得要將所有 PC 的子網路遮罩 (Gateway) 指向 Linux,也就是 192.168.1.254;至於 DNS 就設你的 ISP 所提供的 IP。

至於在 Linux 上,當然就是設 IP 為 192.168.1.254,Subnet Mask 為 255.255.255.0。還有請檢查是否已經安裝 ipchains。如果尚未安裝,請使用 RPM 的指令來安裝,檔案在 RedHat 6.2 的光碟中,檔案名稱為 ipchains-1.3.9-5.i386.rpm。

另外,我們可以在 Linux 建立一個 Shell Script,內容如下所示:

#!/bin/sh
echo "Starting NAT..."
depmod -a
modprobe ip_masq_ftp

echo 1 > /proc/sys/net/ipv4/ip_forward

ipchains -A forward -j MASQ -s 192.168.1.0/24

姑且我們就將檔案命名為 nat,並儲存到 /etc/rc.d 的目錄中。並更改檔案的權限為 755,如下指令所示:

[root@linux rc.d]# chmod 755 /etc/rc.d/nat

最後,修改 /etc/rc.d/rc.local 的內容,以便讓 nat 這個 script 一開機便自動執行。因此我們在 rc.local 檔案的最後面加上如下所示的內容:

if [ -f /etc/rc.d/nat ]; then
  /etc/rc.d/nat
fi

到此設定便完成了,我們可以立刻執行下列的指令來啟動此功能:

[root@linux rc.d]# /etc/rc.d/nat

或將 Linux 重新開機,也是可以的。

OK.. 馬上找部 PC 試試看是否可以正常連到 Internet。

 
解決動態 IP 的問題

目前計時制雖可像專線般使用,可是 ISP 動態分配 IP 是很麻煩的問題,因為別人或當自己在外面時,無法連回 Linux,因為包括你自己在內,你不知道現在的 IP 是什麼。為了解決此一問題,我使用二種方式來克服,雖然沒像固定 IP 那樣方便,但至少還是可以解決變動 IP 的困擾。

使用 Shell Script 來偵測

由於計時制 IP 不固定,因此我首先想到的是:執行 Shell Script 來檢查 IP 是否改變,一但 IP 改變,便發 Email 給特定帳號,這樣即使你人在外面,也可以得知目前的 IP 為何? 以下是我所使用的 Script:

#!/bin/sh
cd /usr/local/ping
rm -rf ping-stop
connect=0 # 0 disconnect 1 connect

while [ ! -f ping-stop ]
do
 connect=`ifconfig ppp0 | grep inet | wc -l`

 if [ $connect -eq 0 ] ; then
  sleep 3
 else
  adslipold=$adslip
  gatewayold=$gateway
  adslip=`ifconfig ppp0 | grep inet | sed -e 's/inet addr://g' | sed -e 's/P-t-P://g' | gawk '{print $1}'`
  gateway=`ifconfig ppp0 | grep inet | sed -e 's/inet addr://g' | sed -e 's/P-t-P://g' | gawk '{print $2}'`
  if [ "$adslip" != "${adslipold}" ] ; then
    echo "`date +%m/%d/%Y` `date +%T`" > connect
    echo $adslip >> connect
    echo $gateway >> connect
    mail `cat maillist` -s Notice-IP更新 < connect
  fi
  sleep 180
 fi
done

我們將以上的 Script 儲存到 /usr/local/ping 的目錄中,檔案名稱為 ipmonitor。另外必須建立一個文字檔,檔名為 maillist。檔案內容為我們要發通知的對象,例如:

snien@kimo.com,abc@hinet.net

最後,依然在 /etc/rc.d/rc.local 的最後,加上以下的段落:

if [ -f /usr/local/ping/ipmonitor ]; then
  /usr/local/ping/ipmonitor &
fi

我們可以執行 /usr/local/ping/ipmonitor & 或重新開機,以便讓此 Script 開始執行。這樣一來,我們只要收 Email 便知道現在的 IP 是多少。

 

向提供動態 DNS 的公司註冊固定的網址

前面所提到的方法雖然可以讓特定的人員知道目前所使用的 IP 位址,但如果要架設伺服器的話,就派不上用場,因為我們無法向全天下的人宣佈最新的 IP 位址,因此我們還是必須要有固定的網址。所幸目前有幾個提供動態 DNS 服務的網站,可以滿足我們的需求,其中 http://www.dyndns.org/http://www.yi.org/ 均是這類型的網站,在本小節中,將以使用 http://www.dyndns.org/ 所提供的服務為例。

首先,要使用 http://www.dyndns.org/ 所提供的服務之前,必須先到 http://members.dyndns.org/ 申請一個帳號。基本上申請的程序還算簡單,因此在此省略說明。假設你已經完成申請的程序,而且順利取得帳號及密碼,接下來請到動態 DNS Client 的網頁 (網址為 http://support.dyndns.org/dyndns/clients/unix.shtml ) 下載所需的 Client 程式。目前已經提供數種 Client 可供選擇,接下來我們以 ddclient 為例。

在開始安裝設定之前,請先確認 Linux 系統是否已經安裝 Perl 5.004 或更新版本。如果尚未安裝,可從 RedHat 6.2 光碟中找到 perl-5.00503-10.i386.rpm 來安裝。接下來開始介紹安裝的程序:

[root@linux /tmp]# tar zxvf ddclient-3.0.2.tar.gz
[root@linux /tmp]# mkdir /root/bin
[root@linux /tmp]# cp ddclient-3.0.2/ddclient /root/bin
[root@linux /tmp]# cp ddclient-3.0.2/sample-etc_ddclient.conf /etc/ddclient.conf
[root@linux /tmp]# cp ddclient-3.0.2/sample-etc_cron.d_ddclient /etc/cron.d/ddclient
[root@linux /tmp]# cp ddclient-3.0.2/sample-etc_rc.d_init.d_ddclient /etc/rc.d/init.d/ddclient
[root@linux /tmp]# chkconfig --add ddclient

編輯 /etc/ddclient.conf 檔案,其內容請參考如下所示範例(請依實際狀況自行修改):

syslog=yes
mail=root
use=if, if=ppp0
protocol=dyndns2
server=members.dyndns.org
login=steven   #申請的帳號
password=yourpassword    #申請的密碼
mx=scw.homeip.net scw.homeip.net    #申請的FQDN

最後執行下列指令以啟動 ddclient:

[root@linux /tmp]# /etc/rc.d/init.d/ddclient start

很簡單吧!! 簡單的小動作,卻每個月可以省下一筆費用,嗯! 還蠻划算的... :-)